package com.xinxin.leetcode.problem538;

import com.xinxin.leetcode.common.TreeNode;
import com.xinxin.leetcode.common.TreeUtil;

/**
 * @author ShiXinXin
 * @date 2020-01-17 9:18
 */
class Solution {
    private Integer pre = null;

    private void postTree(TreeNode root) {
        if (root == null) {
            return;
        }
        postTree(root.right);
        if (pre != null) {
            root.val += pre;
        }
        pre = root.val;
        postTree(root.left);
    }

    public TreeNode convertBST(TreeNode root) {
        postTree(root);
        return root;
    }

    public static void main(String[] args) {
        TreeNode root = TreeUtil.createTree(new int[]{5, 2, 0, 0, 13, 0, 0});
        TreeNode res = new Solution().convertBST(root);
        TreeUtil.inorderPrint(res);
    }
}
